package com.example.cart;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class CategoryActivity extends Activity {
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category);
     // Get the message from the intent
        Intent intent = getIntent();
        int caterogy = intent.getIntExtra(Home.EXTRA_MESSAGE,0);
        List<Product> listAll = ProductHelper.getCatalog();
        List<Product> listFilter = new ArrayList<Product>();
        for(int i=0; i < listAll.size(); i++) {
        	Product item = listAll.get(i);
            //search the string
            if(item.getCategory() == caterogy)
            {
            	listFilter.add(item);
            }
        }
        // Create the List view
        final ListView listViewCatalog = (ListView) findViewById(R.id.ListViewCatalog);
        listViewCatalog.setAdapter(new ProductAdapter(this, listFilter));
        listViewCatalog.setOnItemClickListener(new OnItemClickListener() {
        	public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
        		Intent productDetailsIntent = new Intent(getBaseContext(),ProductDetailActivity.class);
        	    productDetailsIntent.putExtra(ProductHelper.PRODUCT_INDEX, position);
        	    startActivity(productDetailsIntent);
        	}  
        });
        //handler Back Button
        final Button btnBack = (Button) findViewById(R.id.Back);
        btnBack.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	back();
            }
        });
    }
	
	public void back() {
		Intent intent = new Intent(this, Home.class);
    	startActivity(intent);
    }
}
